import { ipcRenderer } from 'electron';

// 发送HTTP请求
export const request = async ({ method, url, data }) => {
  try {
    const response = await ipcRenderer.invoke('http-request', { method, url, data });
    if (response.success) {
      return response.data;
    } else {
      throw new Error(response.error);
    }
  } catch (error) {
    throw error;
  }
};

// 监听认证错误
export const setupAuthErrorHandler = (callback) => {
  const handler = (event, { type, message }) => {
    callback(type, message);
  };
  ipcRenderer.on('auth-error', handler);
  return () => {
    ipcRenderer.removeListener('auth-error', handler);
  };
};